<template>
  <el-container style="min-height: 100vh">

    <el-aside :width="sideWidth + 'px'" style="box-shadow: 2px 0 6px rgb(0 21 41 / 35%);">
      <!--引用侧边栏Aside-->
      <Aside :isCollapse="isCollapse" :logoTextShow="logoTextShow" ></Aside>
    </el-aside>

    <el-container>
      <el-header style="border-bottom: 1px solid #ccc; ">
        <!--引入Header组件-->
        <Header :collapseBtnClass="collapseBtnClass" @asideCollapse="collapse" :user="user"></Header>
      </el-header>

      <el-main>
        <!--引入Main组件 表示当前页面的子路由会在router-view中展示-->
        <router-view @refreshUser="getUserInfo"></router-view>
      </el-main>

    </el-container>
  </el-container>
</template>

<script>
//引入侧边栏组件
import Aside from "@/components/Aside";
//引入头部组件
import Header from "@/components/Header";

export default {
  name: 'Home',
  //组件注册
  components: {
    Aside,
    Header,
  },
  data() {
    return {

      collapseBtnClass: 'el-icon-s-fold',
      isCollapse: false,
      sideWidth: 200,
      logoTextShow: true,
      headerBg: 'headerBg',
      user: {}//获取浏览器用户信息
    }
  },
  created() {
    //从后台获取用户信息最新的User信息
    this.getUserInfo();
  },
  methods: {
    // 收缩侧边栏
    collapse() {  // 点击收缩按钮触发
      this.isCollapse = !this.isCollapse
      if (this.isCollapse) {  // 收缩
        this.sideWidth = 64
        this.collapseBtnClass = 'el-icon-s-unfold'
        this.logoTextShow = false
      } else {   // 展开
        this.sideWidth = 200
        this.collapseBtnClass = 'el-icon-s-fold'
        this.logoTextShow = true
      }
    },
    //获取用户信息
    getUserInfo() {
      let username = localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user')).username : '';
      //获取用户信息从后台获取
      this.request.get('/user/getUserInfo/' + username).then(res => {
        //重新赋值后台的最新User数据
        this.user = res.data
      })
    },
  }


}
</script>


